package com.example.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtils {
    public static final String UPLOADS_PATH = "F:\\workspace\\images";

    public static String uploads(MultipartFile file) throws IOException{
        //文件后缀
        final String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.')+1);

        //文件名
        String fileName = UUID.randomUUID().toString().replace("-","")+"."+fileSuffix;

        //文件写入
        File descFile = new File(UPLOADS_PATH,fileName);
        file.transferTo(descFile);

        //文件url
        String url = "/uploadPic/"+fileName;
        return url;
    }
}
